'use strict';
// 人脸识别视图模块
import {
    titileInputInit
} from '../components/titleInput.js';

var $verifyBtn, $phone, $verification_code, $name;
var daojs = 60;
// 视图初始化
function viewInit() {
    $verifyBtn = $("#verifyBtn");
    $phone = $("#phone");
    $verification_code = $("#verification_code");
    $name = $("#name");
    titileInputInit();
    bindViewEvent();
    $("#fanhui").on("touchstart", function() {
        history.back();
    });
}

// 验证码倒计时
function djsfunc() {
    var handle = setInterval(function() {
        $verifyBtn.text("重新获取(" + daojs + "S)");
        daojs--;
        if (daojs == 0) {
            clearInterval(handle);
            $verifyBtn.attr("weui-btn weui-btn_plain-primary");
            $verifyBtn.text("重新获取验证码");
            daojs = 60;
            $verifyBtn.attr("class", "weui-btn weui-btn_plain-primary");
        }
    }, 1000);
}

// 验证输入框 是否允许提交
function checkedInputValue() {
    var nameVal = $name.val();
    var nameLength = len(nameVal);
    if ($phone.val().length == 11 && $verification_code.val().length > 3 && nameLength>2) {
        $("#submit_btn").attr("class", "weui-btn weui-btn_primary");
    } else {
        $("#submit_btn").attr("class", "weui-btn weui-btn_disabled weui-btn_default ui-link");
    }
}

// 计算姓名长度
function getNameLength() {
    var nameVal = $name.val();
    var nameLength = len(nameVal);
    return nameLength;
}

//取字符串长度，中文算2位
function len(s) {
    var l = 0;
    var a = s.split("");
    for (var i = 0; i < a.length; i++) {
        if (a[i].charCodeAt(0) < 299) {
            l++;
        } else {
            l += 2;
        }
    }
    return l;
}


// 绑定事件
function bindViewEvent() {
    $("#phone").on("input", function() {
        var oldPhone = $(this).val();
        if (oldPhone.length == 11) {
            $verifyBtn.attr("class", "weui-btn weui-btn_plain-primary");
        } else {
            $verifyBtn.attr("class", "weui-btn weui-btn_plain-default weui-btn_plain-disabled");
        }
    });

    $(".weui-cells_form").on("input", checkedInputValue);
}

export {
    viewInit,
    djsfunc
}